// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({env: 'wang2023-5gng88vf3d3ea81f'}) // 使用当前云环境
const db = cloud.database({
  env: 'wang2023-5gng88vf3d3ea81f'
})
const User = db.collection('db_user')
// 头像数组
let usre_images = [
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/1.png?sign=6fcb5f1a97b7b75b4bfbd8b1e1d94260&t=1692163905",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/2.png?sign=5d069f352d476c28c52b0c58ff216521&t=1692163963",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/3.png?sign=21837ecdcd6577eec456402bc0bd81f1&t=1692163975",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/4.png?sign=c798c91ec90d6d99281a1979e500c878&t=1692163991",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/5.png?sign=144c40d781919a862911e53ed9b9f360&t=1692164007",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/6.png?sign=686775b95f9c0177e47e265abc68ff60&t=1692164017",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/7.png?sign=5fba3226ad707c9b2b6895237c18765e&t=1692164027",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/z.png?sign=b3c78d715ea8083f13720d8a18842218&t=1692190698",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/x.png?sign=cd0ae9542a1d413be19698d393b1065c&t=1692190731",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/w.png?sign=db8ef3e0a3eaeab2784dc6ee5b06be5e&t=1692190741",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/p.png?sign=23ace811813b107c81de316160e316a6&t=1692190758",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/o.png?sign=404606240730045a14de4c0b32efdf22&t=1692190766",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/n.png?sign=05c7c6192bf6d3cb1dec99e8f17426be&t=1692190775",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/l.png?sign=014a4fea5329d7e7111ac7413eafcd97&t=1692190784",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/j.png?sign=d5e515b3e2af1623bb995063995f761b&t=1692190797",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/f.png?sign=55830be6fa62190ea4b1a9114a6ab62d&t=1692190806",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/d.png?sign=4ac73893a29f2c0e30d7ed634cf66570&t=1692190815",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/b.png?sign=da23f387f638e2244bcf674f8b822e13&t=1692190826",
  "https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/user/9.png?sign=00ac7036faab2dab0fbbce9b5c73e3cc&t=1692190838"
]

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const openid = wxContext.OPENID
  let index = Math.floor(Math.random() * usre_images.length)
  let picUrl = usre_images[index] //随机头像
  let user_id = Math.random().toString().slice(2);
  user_id = (new Date().getTime().toString().slice(-4))+"_"+user_id;
  // console.log(user_id)
  let {
    avatarUrl,
    nickName
  } = event;
  // 查询用户是否已存在
  let content = await User.where({
      openid
    })
    .get()
  // 是否记录用户信息
  if (content) {
    let data = content.data;
    if (data.length == 0) {
      // 插入账号数据
      let rows = await User.add({
        data: {
          nickName,
          avatarUrl: avatarUrl || picUrl,
          // avatarUrl: picUrl,
          openid,
          _openid:openid,
          create_time: new Date(),
          sex: '',
          age: '',
          title: '',
          address: '',
          qq: '',
          user_id: user_id
        }
      })
      // console.log("2.0", rows);
      if (rows.errMsg == "collection.add:ok") {
        return {
          openid: wxContext.OPENID,
          _openid: wxContext.OPENID,
          code: 200,
          msg: '添加账号成功。',
          openid
        }
      } else {
        return {
          code: -1,
          msg: '添加账号失败。',
        }
      }
    } else {
      return {
        code: 11,
        openid,
        _openid: openid,
        msg: '您的账号已存在。',
        content
      }
    }
  }
}